﻿using UnityEditor;
using UnityEngine;

public class DragAreaGetObject : UnityEditor.Editor
{
    public static TextAsset GetOjbect(string meg = null)
    {
        Event aEvent;
        aEvent = Event.current;

        GUI.contentColor = Color.white;
        Object temp = null;

        var dragArea = GUILayoutUtility.GetRect(0f, 35f, GUILayout.ExpandWidth(true));

        var title = new GUIContent(meg);
        if (string.IsNullOrEmpty(meg)) title = new GUIContent("Drag lua file here from Project view to get the path");

        GUI.Box(dragArea, title);
        switch (aEvent.type)
        {
            case EventType.DragUpdated:
            case EventType.DragPerform:
                if (!dragArea.Contains(aEvent.mousePosition)) break;

                DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
                if (aEvent.type == EventType.DragPerform)
                {
                    DragAndDrop.AcceptDrag();

                    for (var i = 0; i < DragAndDrop.objectReferences.Length; ++i)
                    {
                        temp = DragAndDrop.objectReferences[i];

                        if (temp == null) break;
                    }
                }

                Event.current.Use();
                break;
        }

        return (TextAsset)temp;
    }
}